﻿using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis;
using System.Text;

namespace SourceGeneratorStaticMethod
{
    /// <summary>
    /// 主要实现ISourceGenerator接口的Initialize和Execute方法
    /// Generator 为代码生成器标签
    /// </summary>
    [Generator]
    public class StaticMethodGenerator : ISourceGenerator
    {
        public void Initialize(GeneratorInitializationContext context) { }

        public void Execute(GeneratorExecutionContext context)
        {
            // 新增一个cs文件，文件内容为第二个参数
            context.AddSource("staticMethodFile.cs", SourceText.From(@"
                namespace SourceGeneratorStaticMethod
                {
                    public class StaticMethodClass
                    {
                        public static void StaticMethod()
                        {
                    	     // 命令行输出Hello, StaticMethod!
                             Console.WriteLine(""Hello, StaticMethod!"");
                        }
                    }
                }", Encoding.UTF8));
        }
    }
}
